package kpi.java_training.database;

import kpi.java_training.domain.questions.Topic;
import kpi.java_training.users.UsersList;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Alex Kebkalo (kebka@mail.ru) on 04.03.14.
 */
public class MySqlDataManager implements DataManager {
    private final String CONNECT_STRING = "jdbc:mysql://localhost/teaching_system?user=teaching_system&password=teaching";

    private Connection connect;

    public MySqlDataManager() throws Exception {
        Class.forName("com.mysql.jdbc.Driver");
        connect = DriverManager.getConnection(CONNECT_STRING);
    }

    @Override
    public List<Topic> loadQuestions() throws Exception {
        return null;
    }

    @Override
    public List<Topic> loadQuestions(String user) throws Exception {
        return null;
    }


    @Override
    public List<String> loadAnswers(String user) throws Exception {
        List<String> answers = new ArrayList<>();
        Statement statement = connect.createStatement();
        ResultSet resultSet = statement.executeQuery("select * from learning_answers_history where id_user = "+user);
        while (resultSet.next()) {
            String answer = resultSet.getString("answer");
            answers.add(answer);
        }
        return answers;
    }

    @Override
    public UsersList loadUsers() throws Exception {
        UsersList users = new UsersList();
        Statement statement = connect.createStatement();
        ResultSet resultSet = statement.executeQuery("select * from users");
        while (resultSet.next()) {
            String id = resultSet.getString("id");
            String userName = resultSet.getString("username");
            String password = resultSet.getString("password");
            users.addUser(id, userName, password);
        }
        return users;
    }

    @Override
    public void saveTeachingData(String user, List<Topic> topics, List<String> answers) throws Exception {

    }

    @Override
    public void saveUserData(UsersList users) throws Exception {

    }


}
